package com.qian.saas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qian.saas.domain.Conm;
import com.qian.saas.domain.Part;
import com.qian.saas.domain.Renwu;
import com.qian.saas.domain.Rep;
import com.qian.saas.mapper.ConmMapper;
import com.qian.saas.mapper.PartMapper;
import com.qian.saas.mapper.RenwuMapper;
import com.qian.saas.mapper.RepMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/conm")
@CrossOrigin
public class ConmController {

    @Resource
    private ConmMapper conmMapper;

    @Resource
    private PartMapper partMapper;

    @Resource
    private RepMapper repMapper;

    @PostMapping("/add")
    public R add(@RequestBody Conm conm) {
        System.out.println(conm);
        conmMapper.insert(conm);
        if (conm.getNum() != 0) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("partid", conm.getPartid());
            Part part = partMapper.selectOne(wrapper);
            int nnum = part.getNumsur() - conm.getNum();
            part.setNumsur(nnum);
            //如果库存低于20，修改状态
            if(nnum<20){
                part.setType("库存紧张");
            }
            UpdateWrapper wrapper1=new UpdateWrapper();
            wrapper1.eq("partid",part.getPartid());
            wrapper1.set("numsur",part.getNumsur());
            wrapper1.set("type",part.getType());
            partMapper.update(part, wrapper1);
        }
        QueryWrapper repwrapper = new QueryWrapper();
        repwrapper.eq("repid",conm.getRepid());
        Rep rep = repMapper.selectOne(repwrapper);
        rep.setSt(3);
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("repid",rep.getRepid());
        wrapper.set("uptime", LocalDateTime.now());
        wrapper.set("st",3);
        repMapper.update(rep,wrapper);


        return R.success("成功");
    }

}
